package com.tencent.qplus.paddata.util;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class MailUtils {
	
	//只能在开发网中使用，服务器上需要配置host,172.25.7.178 tsmtp.tencent.com
	private static final String mailServerHost = "tsmtp.tencent.com";
	//private static final String mailServerHost2 = "192.168.1.27";
	private static final String mailServerPort = "25";
	private static final String fromAddress = "qqfropadloganaly@tencent.com";
	private static final String fromName = "QQ for Pad日志分析";
	private static final String userName = "qqfropadloganaly@tencent.com";
	private static final String password = "qq123456ZX";
	private static final String mailer = "sendhtml";
	String subject = "test";
	String content = "test";
	
	private static String DEFAULT_CHARSET = "UTF-8";
	
	public static void sendEmail(String subject,String centent,MailPerson[] to, MailPerson[] cc,MailPerson from) throws UnsupportedEncodingException, MessagingException{
		Properties props = System.getProperties();
		props.put("mail.smtp.host", mailServerHost);
		props.put("mail.smtp.port", mailServerPort);
		props.put("mail.smtp.auth", "true");
		Authenticator authenticator = new Authenticator(){
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(userName,password);
			}
			
		};
		Session session = Session.getDefaultInstance(props, authenticator);
		Message msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress(fromAddress,fromName,DEFAULT_CHARSET));
		for(MailPerson p:to)
			msg.addRecipient(RecipientType.TO, new InternetAddress(p.getMailAddress(), p.getShowName(), DEFAULT_CHARSET));
		
		if(cc!=null && cc.length>0){
			for(MailPerson p:cc){
				msg.addRecipient(RecipientType.CC, new InternetAddress(p.getMailAddress(), p.getShowName(), DEFAULT_CHARSET));
			}
		}
		msg.setSubject(subject);
		msg.setHeader("X-Mailer", mailer);
	    msg.setSentDate(new Date());
	    msg.setText(centent);
	    Transport.send(msg);
	    System.out.println("Mail send success!");
	}
	
	
	public static void sendHtmlEmail(String subject,String centent,MailPerson[] to, MailPerson[] cc,MailPerson from) throws UnsupportedEncodingException, MessagingException{
		
		Properties props = System.getProperties();
		props.put("mail.smtp.host", mailServerHost);
		props.put("mail.smtp.port", mailServerPort);
		props.put("mail.smtp.auth", "true");
		Authenticator authenticator = new Authenticator(){
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(userName,password);
			}
			
		};
		Session session = Session.getDefaultInstance(props, authenticator);
		Message msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress(fromAddress,fromName,DEFAULT_CHARSET));
		for(MailPerson p:to)
			msg.addRecipient(RecipientType.TO, new InternetAddress(p.getMailAddress(), p.getShowName(), DEFAULT_CHARSET));
		
		if(cc!=null && cc.length>0){
			for(MailPerson p:cc){
				msg.addRecipient(RecipientType.CC, new InternetAddress(p.getMailAddress(), p.getShowName(), DEFAULT_CHARSET));
			}
		}
		msg.setSubject(subject);
		msg.setHeader("X-Mailer", mailer);
	    msg.setSentDate(new Date());
	    msg.setContent(surroundWithHtml(subject,centent), "text/html; charset=UTF-8");
	    Transport.send(msg);
	}
	
	public static void sendHtmlEmailWithParts(String subject,String centent,String[] fileParts, MailPerson[] to, MailPerson[] cc,MailPerson from) throws UnsupportedEncodingException, MessagingException{
		Properties props = System.getProperties();
		props.put("mail.smtp.host", mailServerHost);
		props.put("mail.smtp.port", mailServerPort);
		props.put("mail.smtp.auth", "true");
		Authenticator authenticator = new Authenticator(){
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(userName,password);
			}
			
		};
		Session session = Session.getDefaultInstance(props, authenticator);
		Message msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress(fromAddress,fromName,DEFAULT_CHARSET));
		for(MailPerson p:to)
			msg.addRecipient(RecipientType.TO, new InternetAddress(p.getMailAddress(), p.getShowName(), DEFAULT_CHARSET));
		
		if(cc!=null && cc.length>0){
			for(MailPerson p:cc){
				msg.addRecipient(RecipientType.CC, new InternetAddress(p.getMailAddress(), p.getShowName(), DEFAULT_CHARSET));
			}
		}
		msg.setSubject(subject);
		msg.setHeader("X-Mailer", mailer);
	    msg.setSentDate(new Date());
	    Multipart multiparts = new MimeMultipart();
	    MimeBodyPart mms = new MimeBodyPart();
	    mms.setContent(surroundWithHtml(subject,centent), "text/html; charset=UTF-8");
	    multiparts.addBodyPart(mms);
	    for(String filePath:fileParts){
	    	File file = new File(filePath);
	    	if(file.exists() && file.isFile()){
	    		MimeBodyPart mbp = new MimeBodyPart();
	    		try {
					mbp.attachFile(file);
					multiparts.addBodyPart(mbp);
				} catch (IOException e) {
					e.printStackTrace();
				}
	    	}
	    }
	    msg.setContent(multiparts);
	    Transport.send(msg);
	}
	
	private static String surroundWithHtml(String subject,String content){
		StringBuilder sb = new StringBuilder();
		sb.append("<html><head><title>"+subject+"</title></head><body>")
			.append("<h1>").append(subject).append("</h1>")
			.append(content).append("<body>").append("</html>");
		return sb.toString();
	}
}
